/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.lcfgly.Cache;

import com.lcfgly.Interface.ICache;
import java.util.concurrent.TimeoutException;
import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.exception.MemcachedException;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

/**
 *
 * @author lcfgly
 */
@Component("memcache")
public class XMemcache implements ICache<String, Object>{
    @Autowired
            @Qualifier("memcachedClient")
    MemcachedClient xmemcache;
    
    private final long  timeout=3000;
    
    Logger log=Logger.getLogger("Memcache");

    @Override
    public boolean save(String key, Object value) {
        try {
            return xmemcache.set(key, 0, value,timeout );
        } catch (TimeoutException ex) {
            log.warn("保存["+key+"]缓存时间超时:",ex);
        } catch (InterruptedException ex) {
            log.error("线程异常",ex);
        } catch (MemcachedException ex) {
            log.error("缓存错误",ex);
        }
        return false;
    }

    @Override
    public boolean save(String key, Object value, int TTL) {
        try {
            return xmemcache.set(key, TTL, value, timeout);
        } catch (TimeoutException ex) {
            log.warn("保存["+key+"]缓存时间超时:",ex);
        } catch (InterruptedException ex) {
            log.error("线程异常",ex);
        } catch (MemcachedException ex) {
            log.error("缓存错误",ex);
        }
        return false;
    }

    @Override
    public Object get(String key) {
        try {
            return xmemcache.get(key, timeout);
        } catch (TimeoutException ex) {
            log.warn("获取["+key+"]缓存时间超时:",ex);
        } catch (InterruptedException ex) {
            log.error("线程异常",ex);
        } catch (MemcachedException ex) {
            log.error("缓存错误",ex);
        }
        return null;
    }

    @Override
    public boolean remove(String key) {
        try {
            return xmemcache.delete(key,timeout);
        } catch (TimeoutException ex) {
            log.warn("移除["+key+"]缓存时间超时:",ex);
        } catch (InterruptedException ex) {
            log.error("线程异常",ex);
        } catch (MemcachedException ex) {
            log.error("缓存错误",ex);
        }
        return false;
    }

    @Override
    public boolean clear() {
        try {
             xmemcache.flushAll(timeout);
        } catch (TimeoutException ex) {
            log.warn("清空缓存时间超时:",ex);
            return false;
        } catch (InterruptedException ex) {
            log.error("线程异常",ex);
            return false;
        } catch (MemcachedException ex) {
            log.error("缓存错误",ex);
            return false;
        }
        return true;
    }

    @Override
    public boolean replace(String key, Object value,int TTL) {
        try {
            return xmemcache.replace(key, TTL, value, timeout);
        } catch (TimeoutException ex) {
            log.warn("更新缓存["+key+"]超时", ex);
        } catch (InterruptedException ex) {
            log.error("线程异常",ex);
        } catch (MemcachedException ex) {
            log.error("缓存错误",ex);
        }
        return false;
    }
    
}
